﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using l1cs.server.model;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class UBTable
    {
        private static Logger _log = Logger.getLogger( typeof( UBTable ).FullName );

        private static UBTable _instance;

        private Dictionary<int, L1UltimateBattle> _ub = new Dictionary<int, L1UltimateBattle>();

        public static UBTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new UBTable();
            }
            return _instance;
        }

        private UBTable()
        {/*TTOK*/
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                String sqlstr = "SELECT * FROM ub_settings";
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1UltimateBattle ub = new L1UltimateBattle();
                    ub.setUbId( SqlUtils.toInt( item["ub_id"] ) );
                    ub.setMapId( SqlUtils.toShort( item["ub_mapid"] ) );
                    ub.setLocX1( SqlUtils.toInt( item["ub_area_x1"] ) );
                    ub.setLocY1( SqlUtils.toInt( item["ub_area_y1"] ) );
                    ub.setLocX2( SqlUtils.toInt( item["ub_area_x2"] ) );
                    ub.setLocY2( SqlUtils.toInt( item["ub_area_y2"] ) );
                    ub.setMinLevel( SqlUtils.toInt( item["min_lvl"] ) );
                    ub.setMaxLevel( SqlUtils.toInt( item["max_lvl"] ) );
                    ub.setMaxPlayer( SqlUtils.toInt( item["max_player"] ) );
                    ub.setEnterRoyal( Convert.ToBoolean( item["enter_royal"] ) );
                    ub.setEnterKnight( Convert.ToBoolean( item["enter_knight"] ) );
                    ub.setEnterMage( Convert.ToBoolean( item["enter_mage"] ) );
                    ub.setEnterElf( Convert.ToBoolean( item["enter_elf"] ) );
                    ub.setEnterDarkelf( Convert.ToBoolean( item["enter_darkelf"] ) );
                    ub.setEnterDragonKnight( Convert.ToBoolean( item["enter_dragonknight"] ) );
                    ub.setEnterIllusionist( Convert.ToBoolean( item["enter_illusionist"] ) );
                    ub.setEnterMale( Convert.ToBoolean( item["enter_male"] ) );
                    ub.setEnterFemale( Convert.ToBoolean( item["enter_female"] ) );
                    ub.setUsePot( Convert.ToBoolean( item["use_pot"] ) );
                    ub.setHpr( SqlUtils.toInt( item["hpr_bonus"] ) );
                    ub.setMpr( SqlUtils.toInt( item["mpr_bonus"] ) );
                    ub.resetLoc();

                    _ub[ub.getUbId()] = ub;
                }
                // ub_managers load
                sqlstr = "SELECT * FROM ub_managers";
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1UltimateBattle ub = getUb( SqlUtils.toInt( item["ub_id"] ) );
                    if ( ub != null )
                    {
                        ub.addManager( SqlUtils.toInt( item["ub_manager_npc_id"] ) );
                    }
                }
                // ub_times load
                sqlstr = "SELECT * FROM ub_times";
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1UltimateBattle ub = getUb( SqlUtils.toInt( item["ub_id"] ) );
                    if ( ub != null )
                    {
                        ub.addUbTime( SqlUtils.toInt( item["ub_time"] ) );
                    }
                }
            }
            _log.config( "UBリスト " + _ub.Count + "件ロード" );
        }

        /// <summary>
        /// UBのIDからUB情報を返す。
        /// </summary>
        /// <param name="ubId"></param>
        /// <returns></returns>
        public L1UltimateBattle getUb( int ubId )
        {/*TTOK*/
            L1UltimateBattle ret;
            _ub.TryGetValue( ubId, out ret );
            return ret;
        }

        /// <summary>
        /// 全てのUB情報を返す。
        /// </summary>
        /// <returns></returns>
        public L1UltimateBattle[] getAllUb()
        {/*TTOK*/
            return _ub.Values.ToArray();
        }

        /// <summary>
        /// UBマネージャーIDからUB情報を返す。
        /// </summary>
        /// <param name="npcId"></param>
        /// <returns></returns>
        public L1UltimateBattle getUbForNpcId( int npcId )
        {/*TTOK*/
            foreach ( L1UltimateBattle ub in _ub.Values )
            {
                if ( ub.containsManager( npcId ) )
                {
                    return ub;
                }
            }
            return null;
        }

        /// <summary>
        /// 指定されたUBIDに対するパターンの最大数を返す。
        /// </summary>
        /// <param name="ubId">調べるUBID</param>
        /// <returns>パターンの最大数</returns>
        public int getMaxPattern( int ubId )
        {/*TTOK*/
            int n = 0;
            String sqlstr = "SELECT MAX(pattern) FROM spawnlist_ub WHERE ub_id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand(sqlstr);
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = ubId;
                object obj = wrapper.ExecScaler( cmd );
                if ( obj != null && !( obj is DBNull ) )
                {
                    n = Convert.ToInt32( obj );
                }
            }
            return n;
        }
    }
}
